package com.byxy.action;

import com.byxy.model.PartyMemberDate;
import com.byxy.util.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Servlet implementation class PartyMemberDateServlet
 */
@WebServlet("/PartyMemberDateServlet")
public class PartyMemberDateServlet extends HttpServlet {
	Connection conn = null;
    PreparedStatement psta = null;
    private ResultSet rs = null;
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PartyMemberDateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PartyMemberDate pm = null;
		int id = Integer.parseInt(request.getParameter("id"));
		String sql = "SELECT * FROM PartyMemberDate WHERE UserId = ?";
		try {
			conn = DBUtil.getConnection();
			psta = conn.prepareStatement(sql);
			psta.setInt(1, id);
			rs = psta.executeQuery();
			while (rs.next()) {
				String fileName = rs.getString("FileName");
				String filePath = rs.getString("FilePath");
				int uid = rs.getInt("UserId");
				String userName = rs.getString("UserName");
				pm = new PartyMemberDate(fileName, filePath, uid, userName);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				DBUtil.close(rs, psta, conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		request.setAttribute("result", pm);
		request.getRequestDispatcher("PartyMemberDate.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
